package com.beer.beerfriendfinder;

import java.util.ArrayList;

import org.json.JSONException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MessageAdapter extends BaseAdapter{
	
	Context mContext;
	ArrayList<Message> mList;
	String friend_name;
	
	Bitmap bitmap;
	
	Bitmap bitmap_friend;

	public MessageAdapter(Context ct,ArrayList<Message> list){
		super();
		this.mList=list;
		friend_name="";
		mContext = ct;
//		try{//bitmap user
//			if(!SessionUser.path_user_avatar(ct).equals("")){
//				bitmap = BitmapFactory.decodeFile(ct.getFilesDir().getPath().toString()+"/"+SessionUser.path_user_avatar(ct));
//				if(bitmap==null){
//					Log.e("CONTACT ITEM ADAPTER"," KO LAY DUOC BITMAP 1="+ct.getFilesDir().getPath().toString()+"/"+SessionUser.path_user_avatar(ct));
//				}else{
//					bitmap=ImageHelper.getRoundedCornerBitmap(bitmap, 80);
//				}
//			}else{
//				bitmap=null;
//			}
//		}catch(Exception e){
//			
//		}
		
		try{//bitmap friend
			
		}catch(Exception e){
			
		}
		
	}
	
	public MessageAdapter(Context ct,ArrayList<Message> list,String name){
		super();		
		this.mList=list;
		friend_name=name;
		
//		try{
//			if(!SessionUser.path_user_avatar(ct).equals("")){
//				bitmap = BitmapFactory.decodeFile(ct.getFilesDir().getPath().toString()+"/"+SessionUser.path_user_avatar(ct));
//				if(bitmap==null){
//					Log.e("CONTACT ITEM ADAPTER"," KO LAY DUOC BITMAP 1="+ct.getFilesDir().getPath().toString()+"/"+SessionUser.path_user_avatar(ct));
//				}else{
//					bitmap=ImageHelper.getRoundedCornerBitmap(bitmap, 80);
//				}
//			}else{
//				bitmap=null;
//			}
//		}catch(Exception e){
//			
//		}
	}
	
	@Override
	public int getCount() {
		
		return mList.size();
	}

	@Override
	public Object getItem(int arg0) {
		
		return mList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		Message message = (Message) this.getItem(position);
		Log.e("MESSAGE ADAPTER","MESSAGE ADAPTER ID="+message.getIdContact());
		ViewHolder holder; 
		
		holder = new ViewHolder();
		if(message.getIdContact()==0){
			convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_me, parent, false);
		}else if(message.getIdContact()>0){
			convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.message, parent, false);
		}else{
			convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_me, parent, false);
		}
		holder.message = (TextView) convertView.findViewById(R.id.lblMessage);
		holder.date_time=(TextView) convertView.findViewById(R.id.lblTimeChat);
		holder.imgAvatar=(ImageView) convertView.findViewById(R.id.imgAvatarMessage);
		convertView.setTag(holder);
		
		CharSequence cs;
		ImageGetter imageGetter = new ImageGetter() {

			@Override
			public Drawable getDrawable(String source) {						
				
				Drawable drawable;
				int dourceId = mContext.getApplicationContext().getResources()
						.getIdentifier(source, "drawable",
								mContext.getPackageName());

				drawable = mContext.getApplicationContext().getResources()
						.getDrawable(dourceId);

				drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
						drawable.getIntrinsicHeight());

				return drawable;
				
			}
		};
		
		cs = Html.fromHtml(message.getMessage(), imageGetter, null);
		holder.message.setText(cs);
//		holder.date_time.setText(message.getDate_time().substring(10));
		
		
//		if(message.getIdContact()==0){
//			if(bitmap!=null)
//				holder.imgAvatar.setImageBitmap(bitmap);
//		}else if(message.getIdContact()>0){
//			//friend
//			if(bitmap_friend!=null){
//				holder.imgAvatar.setImageBitmap(bitmap_friend);
//			}
//		}else{
//			if(bitmap!=null)
//				holder.imgAvatar.setImageBitmap(bitmap);
//		}
		return convertView;
	}

	private static class ViewHolder
	{
		TextView message;
		TextView date_time;
		ImageView imgAvatar;
	}
	
	public void download_image(final int pos_save,final String linkanh,final Context context){	
//		try{
//			bitmap_friend=ImageHelper.getBitmapFromURL(linkanh);
//			
//			SysData.dataContact.getJSONObject(pos_save).put("avatar", SessionUser.username(context)+"_"+friend_name.replaceAll("\\W", "").replace(" ", "").replaceAll("[\u0000-\u001f]", "")+".jpg");
//			ImageHelper.saveImageToApp(context, bitmap_friend, SessionUser.username(context)+"_"+friend_name.replaceAll("\\W", "").replace(" ", "").replaceAll("[\u0000-\u001f]", ""));			
//		}catch(Exception e){
//			e.printStackTrace();
//		}
//		AsyncTask<Void, Void, Void> download_image=new AsyncTask<Void, Void, Void>(){
//
//			@Override
//			protected Void doInBackground(Void... params) {
//				try{
//					bitmap_friend=ImageHelper.getBitmapFromURL(linkanh);
//					
//				}catch(Exception e){
//					e.printStackTrace();
//				}
//				return null;
//			}
//
//			@Override
//			protected void onPostExecute(Void result) {
//				super.onPostExecute(result);
//				try {
//					if(pos_save!=-1){
//						SysData.dataContact.getJSONObject(pos_save).put("avatar", SessionUser.username(context)+"_"+friend_name.replaceAll("\\W", "").replace(" ", "").replaceAll("[\u0000-\u001f]", "")+".jpg");
//						ImageHelper.saveImageToApp(context, bitmap_friend, SessionUser.username(context)+"_"+friend_name.replaceAll("\\W", "").replace(" ", "").replaceAll("[\u0000-\u001f]", ""));
//					}
//					
//					
//				} catch (JSONException e) {
//					e.printStackTrace();
//				}
//
//			}
//
//		};
//
//		download_image.execute(null, null, null);

	}
	
	public void destroy_image(){
		bitmap.recycle();
		bitmap_friend.recycle();
		System.gc();
	}
	
	public void set_image(String PathAvatar,Context context){
		try{
			bitmap_friend=BitmapFactory.decodeFile(context.getFilesDir().getPath().toString()+"/"+PathAvatar);
			if(bitmap_friend!=null){
				bitmap_friend=ImageHelper.getRoundedCornerBitmap(bitmap_friend, 80);
			}			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
}
